package admin

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
	// name := c.Param("name")
	// password := c.Param("password")
	name := c.DefaultQuery("name", "")
	password := c.DefaultQuery("password", "")

	if name == "admin" && password == "123456" {
		c.String(http.StatusOK, "登陆成功")
	} else {
		c.String(http.StatusOK, "登陆失败")
	}

}

func LoginForm(c *gin.Context) {
	// types := c.DefaultPostForm("type", "post")
	username := c.PostForm("name")
	password := c.PostForm("password")
	c.JSON(http.StatusOK, gin.H{"username": username, "password": password})
	// c.String(http.StatusOK, fmt.Sprintf("username:%s,password:%s", username, password))
}

type User struct {
	Name     string `form:"name"`
	Password string `form:"password"`
}

func LoginForm2(c *gin.Context) {
	// 绑定结构体

	var user User
	// 将request的body中的数据，自动按照json格式解析到结构体
	if err := c.ShouldBindJSON(&user); err != nil {
		// 返回错误信息
		// gin.H封装了生成json数据的工具
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	// 判断用户名密码是否正确
	if user.Name != "admin" || user.Password != "123456" {
		c.JSON(http.StatusBadRequest, gin.H{"status": "304"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"status": "200"})

	// c.String(http.StatusOK, fmt.Sprintf("username:%s,password:%s", username, password))
}
